﻿#ifndef SCHEDULING_WINDOW_H
#define SCHEDULING_WINDOW_H

#include"pcb.h"
#include"myresources.h"
#include"inmemroypcb.h"

#include <QWidget>
#include <QListView>
#include <QStringListModel>
#include <QMessageBox>
#include <QDebug>
#include <QGridLayout>
#include <QAction>

#include <vector>
#include <algorithm> // Needed for swap
#include <iostream>
#include <windows.h>
#include <cstdlib>


namespace Ui {
class Scheduling_Window;
}

class Scheduling_Window : public QWidget
{
    Q_OBJECT

signals:
    void updateLine(int i,QListView *view);
    void clearEditLine();
//  1 is readyLine
//  2 is waitingLine
//  -1 is hangupLine
//  0 is completedLine

public slots:
    void updateLineSlot(int i,QListView *view);
    void clearEditLineSlot();
    void updateIOSlot(QModelIndex);
public:
    explicit Scheduling_Window(QWidget *parent = nullptr);
    ~Scheduling_Window();

    const double MEMORYSIZE = 200.0;
    const int OX = 1590;
    const int XLength = 242;
    const double OY = 65;
    const double ALLHEIGHT = 750.0;

    std::vector<PCB>readyLine;
    std::vector<PCB>waitingLine;
    std::vector<PCB>hangupLine;
    std::vector<PCB>completedLine;
    std::vector<PCB>reserveLine;
    std::vector<inMemroyPCB>inMemoryPCBLine;

    PCB *running = nullptr;
    PCB *editing = nullptr;
    PCB runningProcess;
    PCB editingProcess;
    int continuedJudge = 0;
    int timeLength = 0;
    int nowSelectedRow = -1;
    int memory[200] ;

    int MaxMemorySize = 200;

    QGridLayout *playout = new QGridLayout(this);
    QPushButton *freeMemory ;
    //资源

    bool searchSamePID(QString PID,std::vector<PCB>Line);
    void IOJudge(PCB &);

    PCB* createNewProcess(QString PID,QString name,int needTime,int priorityWeight,int needMemorySize);
    PCB* createNewProcess(QString PID,QString name,int needTime,int priorityWeight,int needMemorySize,QString frontID);
    PCB* createNewProcess(QString PID,QString name,int needTime,int priorityWeight,int needMemorySize,QString frontID,QString backID);

    void sortByPriorityWeight(std::vector<PCB>&);

    void swap(QString &s1,QString &s2);
    void swap(int &s1,int &s2);

    bool memoryJudge(PCB &);

    void updateMemoryLine(inMemroyPCB*);
    void updateMemoryShow(inMemroyPCB*);
    void updateNotUseMemory(int,int);
    void updateUseMemory(int);
    void updateCompletedProcess(PCB *);
    void coutVector();
    void coutMemory();

    void clearRunningInformation();
private slots:
    void on_pushButton_updateProcess_clicked();

    void on_pushButton_goOneTIme_clicked();

    void on_pushButton_automaticOperation_toggled(bool checked);

    void on_pushButton_addOneProcess_clicked();

    void on_pushButton_input_clicked();

    void on_pushButton_randomAddProcess_clicked();

    void on_pushButton_clearList_clicked();

    void on_pushButton_hang_clicked();

private:
    Ui::Scheduling_Window *ui;

    void loadProc();
    void reserveJudge(int rowNum);

    void clearVector(std::vector<PCB>&);
};

#endif // SCHEDULING_WINDOW_H
